﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestProject.ServiceReference1;
using ModelLayer;

namespace TestProject
{
    class Program
    {
        private static WcfFoodAndAllergiesClient client;
        static void Main(string[] args)
        {
            client = new WcfFoodAndAllergiesClient();
            System.Console.WriteLine("#####     Testing applikation for WCF Webservice      #####");
            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void UserTestStart()
        {
            System.Console.WriteLine("**********User testing modul: **********");
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void GetUserTest()
        {
            System.Console.WriteLine("GetUser");
            System.Console.WriteLine("username = tstevens");
            System.Console.WriteLine("userPassword =MaaGodt");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();
            User tUser = null;

            string username = "tstevens";
            string userPassword = "MaaGodt";
            try
            {
                tUser = client.GetUserByUserNameAndPassword(username, userPassword);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("GetUser faild");
                System.Console.WriteLine(e.Message);
            }
            if (tUser == null)
            {
                System.Console.WriteLine("User is empty");
            }
            else
            {
                Console.WriteLine(tUser.UserName);
            }


            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void InsertUserTest()
        {

            System.Console.WriteLine("InsertUser");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            User tuser = new User();
            int ret = -1;
            tuser.Id = 2002;
            tuser.UserPassword = "testpassword";
            tuser.UserName = "testUser";

            string a1 = "Melon";

            Allergies tallergies = new Allergies();
            tallergies = client.GetAllergies(a1);
            tuser.UserAllergies.Add(tallergies);


            try
            {
                ret = client.InsertUser(tuser);
            }
            catch (Exception e)
            {
                e.GetBaseException();
            }

            if (ret > 0)
            {
                System.Console.WriteLine("It's working");
            }
            else
            {
                System.Console.WriteLine("Error!!");
            }
        }
        public void UserTestEnd()
        {
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("User test is complete");
            System.Console.WriteLine("Press Enter to finish");
            System.Console.ReadLine();
        }
        public void RecipeStart()
        {
            System.Console.WriteLine("**********Recipe testing modul: **********");
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void GetRecipeByTypeTest()
        {

            System.Console.WriteLine("GetRecipeByType");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            string input = "Dessert";
            Recipe[] listRecipe;
            listRecipe = new Recipe[75];
            try
            {
                listRecipe = client.GetRecipeByType(input);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("GetRecipeByType faild");
                System.Console.WriteLine(e.Message);
            }
            int count = 0;
            foreach (Recipe r in listRecipe)
            {
                count++;
            }

            System.Console.WriteLine("Found result: " + count);
            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void GetRecipeByIdTest()
        {

            System.Console.WriteLine("GetRecipeById");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            int id = 9;
            Recipe rep = new Recipe();
            try
            {
                rep = client.GetRecipeById(id);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("GetRecipeByType faild");
                System.Console.WriteLine(e.Message);
            }


            System.Console.WriteLine("Found result: " + rep.Name);
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.ReadKey();
        }
        public void GetRecipeByNameTest()
        {

            System.Console.WriteLine("GetRecipeByName");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            string input2 = "Vafler";
            Recipe[] listRecipe2;
            listRecipe2 = new Recipe[75];
            try
            {
                listRecipe2 = client.GetRecipeByName(input2);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("GetRecipeByType faild");
                System.Console.WriteLine(e.Message);
            }
            int count2 = 0;
            foreach (Recipe r in listRecipe2)
            {
                count2++;
            }

            System.Console.WriteLine("Found result: " + count2);
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void GetRecipeByAllergiesTest()
        {

            System.Console.WriteLine("GetRecipeByAllergies");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            List<Allergies> _list = new List<Allergies>();
            string one = "Melon";
            Allergies allergies1 = client.GetAllergies(one);
            _list.Add(allergies1);

            string two = "Krebsdyr";
            Allergies allergies2 = client.GetAllergies(two);
            _list.Add(allergies2);

            string tree = "Bløddyr";
            Allergies allergies3 = client.GetAllergies(tree);
            _list.Add(allergies3);

            string four = "Æg";
            Allergies allergies4 = client.GetAllergies(four);
            _list.Add(allergies4);

            string five = "Fisk";
            Allergies allergies5 = client.GetAllergies(five);
            _list.Add(allergies5);

            string six = "Gulerod";
            Allergies allergies6 = client.GetAllergies(six);
            _list.Add(allergies6);

            Recipe[] listRecipe3;
            listRecipe3 = new Recipe[75];

            Allergies[] allergilist;
            allergilist = new Allergies[6];

            allergilist = _list.ToArray();
            listRecipe3 = client.GetRecipeByAllergi(allergilist);

            int recCount = 0;
            recCount = listRecipe3.Count();

            System.Console.WriteLine("Found recipe with allergise count: " + recCount);
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.ReadLine();
        }
        public void InsertRecipeTest()
        {

            System.Console.WriteLine("InsertRecipe");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Recipe insertRecipe = new Recipe();

            insertRecipe.Name = "testRecipe";
            insertRecipe.Servings = 2;
            insertRecipe.Summary = "Dette er en test opskrift";
            insertRecipe.TypeOf = "Frokost";
            int t1 = 0;
            try
            {
                t1 = client.InsertRecipe(insertRecipe);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("InsertRecipe failed");
                System.Console.WriteLine(e.Message);
            }
            if (t1 > 0)
            {
                System.Console.WriteLine("InsertRecipe worked");
            }
            else
            {
                System.Console.WriteLine("InsertRecipe faild");
            }

            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void SelectAllRecipe()
        {

            System.Console.WriteLine("SelectAllRecipe");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Recipe[] retRecipe;
            retRecipe = new Recipe[100];

            try
            {
                retRecipe = client.SelectAllRecipe();
            }
            catch (Exception e)
            {
                System.Console.WriteLine("SelectAllRecipe faild");
                System.Console.WriteLine(e.Message);
            }
            int countRetRecipe = 0;

            countRetRecipe = retRecipe.Count();

            System.Console.WriteLine("Found result: " + countRetRecipe);


        }
        public void RecipeTestEnd()
        {
            System.Console.WriteLine("");
            System.Console.WriteLine("");

            System.Console.WriteLine("Recipe test is complete");
            System.Console.WriteLine("Press Enter to finish");
            System.Console.ReadLine();
        }
        public void AllergiesTestStart()
        {
            System.Console.WriteLine("**********Allergies testing modul: **********");
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void GetAllegiesTest()
        {

            System.Console.WriteLine("GetAllergies");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();
            string a1 = "Melon";

            Allergies tallergies = new Allergies();
            try
            {
                tallergies = client.GetAllergies(a1);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            if (tallergies == null)
            {
                System.Console.WriteLine("GetAllergies faild");
            }
            else
            {
                System.Console.WriteLine("GetAllergies Succesed");
            }

            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void SelectAllAllergiesTest()
        {

            System.Console.WriteLine("SelectAllAllergies");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();


            Allergies[] retAllergies;
            retAllergies = new Allergies[100];

            try
            {
                retAllergies = client.SelectAllAllergies();
            }
            catch (Exception e)
            {
                System.Console.WriteLine("selectAllAllergies faild");
                System.Console.WriteLine(e.Message);
            }

            int countRetAllergies = 0;

            countRetAllergies = retAllergies.Count();

            System.Console.WriteLine("Found result: " + countRetAllergies);
            System.Console.WriteLine("");
            System.Console.WriteLine("");

        }
        public void InsertAllergies()
        {

            System.Console.WriteLine("InsertAllergies");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Allergies insertAllergiobj = new Allergies();
            insertAllergiobj.Name = "testallergi";
            insertAllergiobj.Summary = "Dette er en insert allergi test";

            int insertAllergi = 0;
            try
            {
                insertAllergi = client.InsertAllergies(insertAllergiobj);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("InsertAllergies faild");
                System.Console.WriteLine(e.Message);
            }
            if (insertAllergi > 0)
            {
                System.Console.WriteLine("InsertAllergies work");
            }
            else
            {
                System.Console.WriteLine("InsertAllergies faild");
            }
        }
        public void AllergiesTestEnd()
        {
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("Allergies test is complete");
            System.Console.WriteLine("Press Enter to finish");
            System.Console.ReadLine();
        }
        public void ProductStartTest()
        {
            System.Console.WriteLine("**********Product testing modul: **********");
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void InsertProductTest()
        {
            System.Console.WriteLine("InsertProduct");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Product inProduct = new Product();

            inProduct.Name = "testnavn";
            inProduct.productType = "testType";
            inProduct.photoId = 1;

            string a1 = "Melon";

            Allergies tallergies = new Allergies();

            tallergies = client.GetAllergies(a1);

            inProduct.Allergies.Add(tallergies);
            int productCount = 0;
            
            try
            {
                productCount = client.InsertProduct(inProduct);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Insert Product faild");
                System.Console.WriteLine(e.Message);
            }
            System.Console.WriteLine("");
            System.Console.WriteLine("");
        }
        public void GetProductByNameTest()
        {
            System.Console.WriteLine("GetProductByName");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();


            Product[] retProducts;
            retProducts = new Product[100];
            string insertstring = "rismælk";
            try
            {
                retProducts = client.GetProductByName(insertstring);
            }
            catch(Exception e)
            {
                System.Console.WriteLine("GetProductByName faild");
                System.Console.WriteLine(e.Message);
            }
        }
        public void GetProductByTypeTest()
        {
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("GetProductByType");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Product[] retProducts2;
            retProducts2 = new Product[100];
            string insert2 = "Kølevare";
            try
            {
                retProducts2 = client.GetProductByType(insert2);
            }
            catch(Exception e)
            {
                System.Console.WriteLine("GetProductByType faild");
                System.Console.WriteLine(e.Message);
            }
            System.Console.WriteLine("Found result: " + retProducts2.Count());
            System.Console.WriteLine();
            System.Console.ReadLine();
        }
        public void SelectAllProductTest()
        {

            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("SelectAllProduct");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Product[] allProducts;
            allProducts = new Product[100];

            try
            {
                allProducts = client.SelectAllProduct();
            }
            catch(Exception e)
            {
                System.Console.WriteLine("SelectAllProduct faild");
                System.Console.WriteLine(e.Message);
            }
            System.Console.WriteLine("Found result: " + allProducts.Count());
            System.Console.ReadLine();
        }
        public void GetProductBtAllergiesTest()
        {

            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("GetProductByAllergies");
            System.Console.WriteLine("Press anykey to start");
            System.Console.ReadKey();

            Allergies tallergies2 = new Allergies();

            tallergies2 = client.GetAllergies("melon");

            List<Allergies> list2 = new List<Allergies>();
            list2.Add(tallergies2);

            Product[] retProductByAllergies;
            retProductByAllergies = new Product[100];

            try
            {
                retProductByAllergies = client.GetProductByAllergies(list2.ToArray());
            }
            catch(Exception e)
            {
                System.Console.WriteLine("GetProductByAllergies faild");
                System.Console.WriteLine(e.Message);
            }
           
        }
        public void ProductTestEnd()
        {
            System.Console.WriteLine("");
            System.Console.WriteLine("");
            System.Console.WriteLine("Product test is complete");
            System.Console.WriteLine("Press Enter to finish");
            System.Console.ReadLine();
        }


    }
}
